package com.insyte.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.insyte.R;
import com.insyte.fragments.CartFragment;
import com.insyte.fragments.FindUserFragment;
import com.insyte.fragments.HomeFragment;
import com.insyte.fragments.SettingFragment;
import com.insyte.fragments.UserFragment;

public class TabMainActivity extends FragmentActivity implements View.OnClickListener {

    private RelativeLayout btn_More, btn_Cart, btn_Search, btn_User, btn_Home;
    private ImageView img_more, img_cart, img_search, img_user, img_home;
    private LinearLayout bg_hover_home, bg_hover_user, bg_hover_search, bg_hover_cart, bg_hover_setting;
    private LinearLayout btn_Setting,btn_Help;
    FragmentManager manager;
    FragmentTransaction transaction;
    Drawable new_image;
    LinearLayout lnExternalMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        getView();
        defaultStart();
    }

    private void getView() {
        btn_More = (RelativeLayout) findViewById(R.id.btn_More);
        btn_Cart = (RelativeLayout) findViewById(R.id.btn_Cart);
        btn_Search = (RelativeLayout) findViewById(R.id.btn_Search);
        btn_User = (RelativeLayout) findViewById(R.id.btn_User);
        btn_Home = (RelativeLayout) findViewById(R.id.btn_Home);

        img_more = (ImageView) findViewById(R.id.img_more);
        img_cart = (ImageView) findViewById(R.id.img_cart);
        img_search = (ImageView) findViewById(R.id.img_search);
        img_user = (ImageView) findViewById(R.id.img_user);
        img_home = (ImageView) findViewById(R.id.img_home);

        lnExternalMenu = (LinearLayout) findViewById(R.id.lnExternalMenu);
        btn_Help = (LinearLayout) findViewById(R.id.btn_Help);
        btn_Setting = (LinearLayout) findViewById(R.id.btn_Setting);
        bg_hover_home = (LinearLayout) findViewById(R.id.bg_hover_home);
        bg_hover_user = (LinearLayout) findViewById(R.id.bg_hover_user);
        bg_hover_search = (LinearLayout) findViewById(R.id.bg_hover_search);
        bg_hover_cart = (LinearLayout) findViewById(R.id.bg_hover_cart);
        bg_hover_setting = (LinearLayout) findViewById(R.id.bg_hover_setting);

        lnExternalMenu.setOnClickListener(this);
        btn_More.setOnClickListener(this);
        btn_Help.setOnClickListener(this);
        btn_Setting.setOnClickListener(this);
        btn_Cart.setOnClickListener(this);
        btn_Search.setOnClickListener(this);
        btn_User.setOnClickListener(this);
        btn_Home.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_More:
                if (lnExternalMenu.getVisibility()!=View.VISIBLE)
                lnExternalMenu.setVisibility(View.VISIBLE);
                else lnExternalMenu.setVisibility(View.GONE);
//                transaction = manager.beginTransaction();
//                transaction.replace(R.id.container1, new SettingFragment(), "myFragment");
//                transaction.addToBackStack(null);
//                transaction.commit();

                btn_More.setBackgroundColor(Color.parseColor("#cccccc"));
                btn_Cart.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Search.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_User.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Home.setBackgroundColor(Color.parseColor("#e1e1e1"));

                img_more.setImageResource(R.drawable.icon_setting_orange);
                bg_hover_setting.setBackgroundColor(getResources().getColor(R.color.primary_orange));
                img_cart.setImageResource(R.drawable.icon_cart);
                bg_hover_cart.setBackgroundColor(Color.parseColor("#ababab"));
                img_search.setImageResource(R.drawable.icon_search);
                bg_hover_search.setBackgroundColor(Color.parseColor("#ababab"));
                img_user.setImageResource(R.drawable.icon_user);
                bg_hover_user.setBackgroundColor(Color.parseColor("#ababab"));
                img_home.setImageResource(R.drawable.icon_home);
                bg_hover_home.setBackgroundColor(Color.parseColor("#ababab"));

                break;
            case R.id.btn_Setting:
                if (lnExternalMenu.getVisibility()==View.VISIBLE)
                    lnExternalMenu.setVisibility(View.GONE);
                transaction = manager.beginTransaction();
                transaction.replace(R.id.container1, new SettingFragment(), "myFragment");
                transaction.addToBackStack(null);
                transaction.commit();
                btn_More.setBackgroundColor(Color.parseColor("#cccccc"));
                btn_Cart.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Search.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_User.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Home.setBackgroundColor(Color.parseColor("#e1e1e1"));
                img_more.setImageResource(R.drawable.icon_setting_orange);
                bg_hover_setting.setBackgroundColor(getResources().getColor(R.color.primary_orange));
                img_cart.setImageResource(R.drawable.icon_cart);
                bg_hover_cart.setBackgroundColor(Color.parseColor("#ababab"));
                img_search.setImageResource(R.drawable.icon_search);
                bg_hover_search.setBackgroundColor(Color.parseColor("#ababab"));
                img_user.setImageResource(R.drawable.icon_user);
                bg_hover_user.setBackgroundColor(Color.parseColor("#ababab"));
                img_home.setImageResource(R.drawable.icon_home);
                bg_hover_home.setBackgroundColor(Color.parseColor("#ababab"));

                break;
            case R.id.btn_Cart:
                if (lnExternalMenu.getVisibility() == View.VISIBLE)
                    lnExternalMenu.setVisibility(View.GONE);
                transaction = manager.beginTransaction();
                transaction.replace(R.id.container1, new CartFragment());
                transaction.addToBackStack(null);
                transaction.commit();

                btn_More.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Cart.setBackgroundColor(Color.parseColor("#cccccc"));
                btn_Search.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_User.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Home.setBackgroundColor(Color.parseColor("#e1e1e1"));

                img_more.setImageResource(R.drawable.icon_setting);
                bg_hover_setting.setBackgroundColor(Color.parseColor("#ababab"));
                img_cart.setImageResource(R.drawable.icon_cart);
                bg_hover_cart.setBackgroundColor(getResources().getColor(R.color.primary_orange));
                img_search.setImageResource(R.drawable.icon_search);
                bg_hover_search.setBackgroundColor(Color.parseColor("#ababab"));
                img_user.setImageResource(R.drawable.icon_user);
                bg_hover_user.setBackgroundColor(Color.parseColor("#ababab"));
                img_home.setImageResource(R.drawable.icon_home);
                bg_hover_home.setBackgroundColor(Color.parseColor("#ababab"));
                break;
            case R.id.btn_Search:
                if (lnExternalMenu.getVisibility() == View.VISIBLE)
                    lnExternalMenu.setVisibility(View.GONE);
                transaction = manager.beginTransaction();
                transaction.replace(R.id.container1, new FindUserFragment());
                transaction.addToBackStack(null);
                transaction.commit();

                btn_More.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Cart.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Search.setBackgroundColor(Color.parseColor("#cccccc"));
                btn_User.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Home.setBackgroundColor(Color.parseColor("#e1e1e1"));

                img_more.setImageResource(R.drawable.icon_setting);
                bg_hover_setting.setBackgroundColor(Color.parseColor("#ababab"));
                img_cart.setImageResource(R.drawable.icon_cart);
                bg_hover_cart.setBackgroundColor(Color.parseColor("#ababab"));
                img_search.setImageResource(R.drawable.icon_search_orange);
                bg_hover_search.setBackgroundColor(getResources().getColor(R.color.primary_orange));
                img_user.setImageResource(R.drawable.icon_user);
                bg_hover_user.setBackgroundColor(Color.parseColor("#ababab"));
                img_home.setImageResource(R.drawable.icon_home);
                bg_hover_home.setBackgroundColor(Color.parseColor("#ababab"));
                break;
            case R.id.btn_User:
                if (lnExternalMenu.getVisibility() == View.VISIBLE)
                    lnExternalMenu.setVisibility(View.GONE);
                transaction = manager.beginTransaction();
                transaction.replace(R.id.container1, new UserFragment());
                transaction.addToBackStack(null);
                transaction.commit();

                btn_More.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Cart.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Search.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_User.setBackgroundColor(Color.parseColor("#cccccc"));
                btn_Home.setBackgroundColor(Color.parseColor("#e1e1e1"));

                img_more.setImageResource(R.drawable.icon_setting);
                bg_hover_setting.setBackgroundColor(Color.parseColor("#ababab"));
                img_cart.setImageResource(R.drawable.icon_cart);
                bg_hover_cart.setBackgroundColor(Color.parseColor("#ababab"));
                img_search.setImageResource(R.drawable.icon_search);
                bg_hover_search.setBackgroundColor(Color.parseColor("#ababab"));
                img_user.setImageResource(R.drawable.icon_user);
                bg_hover_user.setBackgroundColor(getResources().getColor(R.color.primary_orange));
                img_home.setImageResource(R.drawable.icon_home);
                bg_hover_home.setBackgroundColor(Color.parseColor("#ababab"));
                break;
            case R.id.btn_Home:
                if (lnExternalMenu.getVisibility() == View.VISIBLE)
                    lnExternalMenu.setVisibility(View.GONE);
                transaction = manager.beginTransaction();
                transaction.replace(R.id.container1, new HomeFragment());
                transaction.addToBackStack(null);
                transaction.commit();

                btn_More.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Cart.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Search.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_User.setBackgroundColor(Color.parseColor("#e1e1e1"));
                btn_Home.setBackgroundColor(Color.parseColor("#cccccc"));

                img_more.setImageResource(R.drawable.icon_setting);
                bg_hover_setting.setBackgroundColor(Color.parseColor("#ababab"));
                img_cart.setImageResource(R.drawable.icon_cart);
                bg_hover_cart.setBackgroundColor(Color.parseColor("#ababab"));
                img_search.setImageResource(R.drawable.icon_search);
                bg_hover_search.setBackgroundColor(Color.parseColor("#ababab"));
                img_user.setImageResource(R.drawable.icon_user);
                bg_hover_user.setBackgroundColor(Color.parseColor("#ababab"));
                img_home.setImageResource(R.drawable.icon_home_orange);
                bg_hover_home.setBackgroundColor(getResources().getColor(R.color.primary_orange));
                break;
        }
    }
    private void addMapFragment() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        HomeFragment fragment = new HomeFragment();
        transaction.add(R.id.mapView, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
    private void defaultStart() {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.container1, new HomeFragment(), "myFragment");
        transaction.addToBackStack(null);
        transaction.commit();
        //addMapFragment();//
        btn_More.setBackgroundColor(Color.parseColor("#e1e1e1"));
        btn_Cart.setBackgroundColor(Color.parseColor("#e1e1e1"));
        btn_Search.setBackgroundColor(Color.parseColor("#e1e1e1"));
        btn_User.setBackgroundColor(Color.parseColor("#e1e1e1"));
        btn_Home.setBackgroundColor(Color.parseColor("#cccccc"));

        img_more.setImageResource(R.drawable.icon_setting);
        bg_hover_setting.setBackgroundColor(Color.parseColor("#ababab"));
        img_cart.setImageResource(R.drawable.icon_cart);
        bg_hover_cart.setBackgroundColor(Color.parseColor("#ababab"));
        img_search.setImageResource(R.drawable.icon_search);
        bg_hover_search.setBackgroundColor(Color.parseColor("#ababab"));
        img_user.setImageResource(R.drawable.icon_user);
        bg_hover_user.setBackgroundColor(Color.parseColor("#ababab"));
        img_home.setImageResource(R.drawable.icon_home_orange);
        bg_hover_home.setBackgroundColor(getResources().getColor(R.color.primary_orange));
    }
}
